// server/app.js
// 后台接口服务
const express = require("express");
const multer = require("multer");
const path = require("path");
const upload = multer({ dest: "uploads/" });
const app = express();

app.use(require("cors")());
app.use("/uploads", express.static(path.join(__dirname, "./uploads")));
app.get("/", (req, res) => {
  res.send("返回Hello World");
});
// // 前端传递的文件类型必须和后端接口的解析类型保持一致
// // 如：后台设置解析类型为 upload.single("pic")，name前端传递的文件类型必须是pic
app.post("/upload", upload.single("pic"), function (req, res, next) {
  console.log(path.join("http://127.0.0.1:8888", req.file.path));
  // 上传图片成功之后给客户端返回图片的地址
  res.send({
    url: path.join("http://127.0.0.1:8888", req.file.path),
  });
});

app.listen(8888, () => {
  console.log("http://127.0.0.1:8888");
});
